package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.PrepayWebRequest;
import sz.szsmk.citizencard.model.network.bean.request.ServiceNetReq;
import sz.szsmk.citizencard.model.network.bean.request.ServiceSearchReq;
import sz.szsmk.citizencard.model.network.bean.response.PrepayResponse;
import sz.szsmk.citizencard.model.network.bean.response.ServiceTypeRes;
import sz.szsmk.citizencard.model.network.bean.response.WebsiteTypeRes;
import sz.szsmk.citizencard.mvp.contract.ISerNetworkContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.PubUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/12 9:21
 * description:
 */
public class SerNetworkPresenter extends IBasePresenter
        implements ISerNetworkContact.ISerNetworkPresenter<ISerNetworkContact.ISerNetworkView> {

    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public SerNetworkPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    @Override
    public void login(String mLatitude, String mLongitude) {
        PrepayWebRequest prepayWebRequest = new PrepayWebRequest();
        prepayWebRequest.setLatitude(mLatitude);
        prepayWebRequest.setLongitude(mLongitude);
        prepayWebRequest.setPage_num("1");
        prepayWebRequest.setPage_size("15");
        String json = new Gson().toJson(prepayWebRequest);
        L.e(Constant.TAG, json);
        mNetworkDateSource.prepayWebsiteList(prepayWebRequest, new SubscribeResult<PrepayResponse>() {
            @Override
            protected void onOk(PrepayResponse response) {
                if (response != null) {
                    String json = new Gson().toJson(response);
                    L.e(Constant.TAG, "prepayWebsiteList响应:" + json);
                    ((ISerNetworkContact.ISerNetworkView) mView).getListSuccess(response);
                }

            }

            @Override
            protected void onFailure(PrepayResponse response) {
                super.onFailure(response);

            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void queryServiceTypeApp() {
        OkHttpUtils.post()
                .url(Constant.BASE_URL + "app/interface/queryServiceTypeApp")
                .addParams("origin", "android")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        if (response != null) {
                            L.e(Constant.TAG, "ServiceTypeRes----" + response);
                            ServiceTypeRes serviceTypeRes = new Gson().fromJson(response, ServiceTypeRes.class);
                            ((ISerNetworkContact.ISerNetworkView) mView).queryServiceTypeAppSucc(serviceTypeRes);
                        }
                    }
                });
    }

    @Override
    public void queryWebsiteTypeApp() {
        OkHttpUtils.post()
                .url(Constant.BASE_URL + "app/interface/queryWebsiteTypeApp")
                .addParams("origin", "android")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        if (response != null) {
                            L.e(Constant.TAG, "ServiceTypeRes----" + response);
                            WebsiteTypeRes service = new Gson().fromJson(response, WebsiteTypeRes.class);
                            ((ISerNetworkContact.ISerNetworkView) mView).queryWebsiteTypeAppSucc(service);
                        }
                    }
                });
    }

    @Override
    public void getAllList(String service_type, String site_type, String distance_type, int type, String mLatitude, String mLongitude, int pager) {
        ServiceNetReq serviceNetReq = new ServiceNetReq();
        serviceNetReq.setLatitude(mLatitude);
        serviceNetReq.setLongitude(mLongitude);
        serviceNetReq.setDistance_type(distance_type);
        serviceNetReq.setPage_num(pager + "");
        serviceNetReq.setPage_size("15000");
        serviceNetReq.setService_type(service_type);
        serviceNetReq.setSite_type(site_type);
        String json = new Gson().toJson(serviceNetReq);
        L.e(Constant.TAG, "getWebsiteList----" + json);
        mNetworkDateSource.websiteList(serviceNetReq, new SubscribeResult<PrepayResponse>() {
            @Override
            protected void onOk(PrepayResponse response) {

                String toJson = new Gson().toJson(response);
                L.e(Constant.TAG, "getWebsiteList----res" + toJson);
                List<PrepayResponse.WebsiteListInfoBean> websiteListInfo = response.getWebsiteListInfo();
                ((ISerNetworkContact.ISerNetworkView) mView).getAllListSucc(websiteListInfo);
            }

            @Override
            protected void onFailure(PrepayResponse response) {
                super.onFailure(response);
                ((ISerNetworkContact.ISerNetworkView) mView).getAllListFail();

            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((ISerNetworkContact.ISerNetworkView) mView).getAllListFail();

            }
        });
    }

    @Override
    public void serviceSearch(String type, String mLatitude, String mLongitude, String pagenum, String pagesize) {
        ServiceSearchReq serviceSearchReq = new ServiceSearchReq();
        serviceSearchReq.setKeyWords(type);
        serviceSearchReq.setLatitude(mLatitude);
        serviceSearchReq.setLongitude(mLongitude);
        serviceSearchReq.setPage_num(pagenum);
        serviceSearchReq.setPage_size(pagesize);
        mNetworkDateSource.serviceSearch(serviceSearchReq, new SubscribeResult<PrepayResponse>() {
            @Override
            protected void onOk(PrepayResponse response) {

            }

            @Override
            protected void onNext(PrepayResponse response) {
                super.onNext(response);
                PubUtils.popTipOrWarn(DaggerApplication.getIns(), response.getMessage());
            }

            @Override
            protected void onFailure(PrepayResponse response) {
                super.onFailure(response);
                PubUtils.popTipOrWarn(DaggerApplication.getIns(), response.getMessage());
                ((ISerNetworkContact.ISerNetworkView) mView).serviceSearchFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

                PubUtils.popTipOrWarn(DaggerApplication.getIns(), throwable.getMessage());
            }
        });
    }

    @Override
    public void getWebsiteList(String service_type, String site_type, String distance_type, int type, String mLatitude, String mLongitude, int pager) {
        ServiceNetReq serviceNetReq = new ServiceNetReq();
        serviceNetReq.setLatitude(mLatitude);
        serviceNetReq.setLongitude(mLongitude);
        serviceNetReq.setDistance_type(distance_type);
        serviceNetReq.setPage_num(pager + "");
        serviceNetReq.setPage_size("15");
        serviceNetReq.setService_type(service_type);
        serviceNetReq.setSite_type(site_type);
        String json = new Gson().toJson(serviceNetReq);
        L.e(Constant.TAG, "getWebsiteList----" + json);
        mNetworkDateSource.websiteList(serviceNetReq, new SubscribeResult<PrepayResponse>() {
            @Override
            protected void onOk(PrepayResponse response) {

                String toJson = new Gson().toJson(response);
                L.e(Constant.TAG, "getWebsiteList----res" + toJson);
                List<PrepayResponse.WebsiteListInfoBean> websiteListInfo = response.getWebsiteListInfo();
                ((ISerNetworkContact.ISerNetworkView) mView).getWebsiteListSucc(websiteListInfo);
            }

            @Override
            protected void onFailure(PrepayResponse response) {
                super.onFailure(response);
                ((ISerNetworkContact.ISerNetworkView) mView).getWebsiteListFail();

            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((ISerNetworkContact.ISerNetworkView) mView).getWebsiteListFail();

            }
        });
    }

}
